<?php
require_once APP . '/application/admincontrollers/AdminAction.php';
/**
 * 后台管理主页
 * @package admin
 */
class AdController extends AdminAction
{
    
	/**
	 * Enter description here...
	 *
	 * @var AdlistModel
	 */
	private $ad;
    public function init()
    {
    	require_once(APP . '/application/models/AdlistModel.php');
    	
        $this->ad = new AdlistModel($this->db);
    }


    /**
     * 文章管理
     *
     */
    public function listAction()
    {
    	$per_page = 50;
    	
    	$result = $this->ad->fetchAll($nowp, $per_page);
    	
    	$this->view->nowp = $nowp;
        $this->view->list = $result['list'];
        $this->view->page = $result['page'];
    }
    
    
    public function editAction()
    {
    	$id = (int) $this->getParam("id");
    	$nowp = (int) $this->getParam("nowp");
    	$content = $this->ad->show($id);
    	        
        $this->view->nowp = $nowp;
        $this->view->info = $content;
    }
    
    public function doeditAction()
    {
    	$id = $this->getParam("id");
    	$params = $this->getAllParams();
    	
    	/**************图片处理*********************/
    	$image = "";
    	if (!empty($_FILES) && !empty($_FILES['image']) && !empty($_FILES['image']['name'])){
    		$file_ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
    		$todir = APP . '/upload';
    		if (!file_exists($todir . '/image'))
    			mkdir($todir . '/image');
    		if (!file_exists($todir . '/image/' . date("Ymd")))
    			mkdir($todir . '/image/' . date("Ymd"));
    		
    		$image = '/upload/image/' . date("Ymd") . '/' .date("YmdHis") . '_' . uniqid("h") . '.' . $file_ext;
    		move_uploaded_file($_FILES['image']['tmp_name'], APP . $image);
    	}
    	if (!empty($image))
    		$params['image'] = $image;
    	
    	$nowp = $this->getParam("nowp");
    	$this->ad->change($id, $params);
    	
    	$this->view->msg = '编辑成功';
    	$this->view->url = $this->url($this->view->baseUrl, "ad", "list", "nowp=" . $nowp, "admin");
    	$this->showmessage();
    }
    
}